package com.ruoyi.project.business.monthschedule.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.model.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.monthschedule.entity.MonthSchedule;
import com.ruoyi.project.business.monthschedule.request.AiAssignRequest;
import com.ruoyi.project.business.monthschedule.request.MonthScheduleListOfDeptRequest;
import com.ruoyi.project.business.monthschedule.request.MonthScheduleSaveOrUpdateRequest;
import com.ruoyi.project.business.monthschedule.response.MonthScheduleResponse;
import com.ruoyi.project.business.monthschedule.service.IMonthScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 月排班Controller
 * 
 * @author ${author}
 * @date 2024-02-05
 */
@RestController
@RequestMapping("/business/monthschedule")
public class MonthScheduleController extends BaseController {
    @Autowired
    private IMonthScheduleService monthScheduleService;

    /**
     * 查询月排班列表
     */
    @PreAuthorize("@ss.hasPermi('business:monthschedule:list')")
    @GetMapping("/list-of-dept")
    public TableDataInfo listOfDept(MonthScheduleListOfDeptRequest request) {
        startPage();
        Date yearMonth = request.getYearMonth();
        Long deptId = request.getDeptId();
        // 查指定员工的排班表
        Long userId = request.getUserId();
        Long tenantId = getTenantId();
        List<MonthScheduleResponse> list = monthScheduleService.selectMonthScheduleListWithUser(yearMonth, userId, deptId, tenantId);
        return getDataTable(list);
    }

    /**
     * 导出月排班列表
     */
    @PreAuthorize("@ss.hasPermi('business:monthschedule:export')")
    @Log(title = "月排班", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MonthSchedule monthSchedule) {
        Long tenantId = getTenantId();
        List<MonthSchedule> list = monthScheduleService.selectMonthScheduleList(monthSchedule, tenantId);
        ExcelUtil<MonthSchedule> util = new ExcelUtil<MonthSchedule>(MonthSchedule.class);
        return util.exportExcel(list, "monthschedule");
    }

    /**
     * 获取月排班详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:monthschedule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(monthScheduleService.selectMonthScheduleById(id));
    }

    /**
     * 新增月排班
     */
    @PreAuthorize("@ss.hasPermi('business:monthschedule:add')")
    @Log(title = "月排班", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult saveOrUpdate(@RequestBody MonthScheduleSaveOrUpdateRequest request) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        Long id = monthScheduleService.saveOrUpdate(request, operatorId, tenantId);
        return toAjax(1);
    }

    /**
     * 智能月排班
     */
    @PreAuthorize("@ss.hasPermi('business:monthschedule:add')")
    @Log(title = "智能月排班", businessType = BusinessType.INSERT)
    @PostMapping("/ai")
    public AjaxResult aiAssign(@RequestBody AiAssignRequest request) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        monthScheduleService.aiAssign(request, operatorId, tenantId);
        return toAjax(1);
    }

    /**
     * 修改月排班
     */
    @PreAuthorize("@ss.hasPermi('business:monthschedule:edit')")
    @Log(title = "月排班", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MonthSchedule monthSchedule) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        return toAjax(monthScheduleService.updateMonthSchedule(monthSchedule, operatorId, tenantId));
    }

    /**
     * 删除月排班
     */
    @PreAuthorize("@ss.hasPermi('business:monthschedule:remove')")
    @Log(title = "月排班", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(monthScheduleService.deleteMonthScheduleByIds(ids));
    }
}
